/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.game.stepbystep;

import java.io.IOException;

/**
 *
 * @author zinin
 */
public class StateCharacterDirected extends State{
    private int xendpoint = 0;
    private int yendpoint = 0;
    private final int increment = MIDPCanvas.GRID_WIDTH / 3;
    private GameLayerManager context;
    
    StateCharacterDirected(GameLayerManager aThis) {
        context = aThis;
    }

    public void onCanvasDrawing(){
        try {
            context.gameDesign.getSelmark().setVisible(false);
            // calculations are in screen coordinates
            int xFatman = context.gameDesign.getFatman().getX() - context.displayOriginX();
            int yFatman = context.gameDesign.getFatman().getY() - context.displayOriginY();
            
            int xpos = xFatman;
            int ypos = yFatman;
            
            if(Math.abs(xendpoint - xFatman) > increment){
                xpos = (xendpoint - xFatman > 0) ? xFatman + increment : xFatman - increment;
            }
                
            if(Math.abs(yendpoint - yFatman) > increment){
                ypos = (yendpoint - yFatman > 0) ? yFatman + increment : yFatman - increment;
            }

            if(Math.abs(xpos - xFatman) < increment && Math.abs(ypos - yFatman) < increment){
                context.setState(context.STATE_IDLE);
            }else{
                context.setFatmanPos(xpos, ypos);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public boolean isAllowDragging() {
        return false;
    }

    public void onTapScreen(int xScreenPos, int yScreenPos) {
        xendpoint = xScreenPos;
        yendpoint = yScreenPos;
    }

    public String id() {
        return "directed";
    }
}
